package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
	"reflect"
	"strconv"
	"strings"
	"time"
)

func main() {
	os.Exit(0)
	MyHero()
	rand.Seed(time.Now().UnixNano())
	fmt.Println(rand.Intn(9000))
	fmt.Println(rand.Intn(9000))
	fmt.Println(rand.Intn(9000))
	fmt.Println(rand.Intn(9000))
	os.Exit(0)
	//arr := [...]int{23, 14, 90, 67, 82, 100, 12}
	//QuickSort(0, 6, &arr)
	//fmt.Println("ok")
	stack := &Stack{
		MaxTop: 5,
		Top:    -1,
	}
	stack.Push(1)
	stack.Push(2)
	stack.Push(3)
	stack.Push(4)
	stack.List()
	fmt.Println()
	fmt.Println("出栈val=", stack.Pop())
	fmt.Println("出栈val=", stack.Pop())
	fmt.Println("出栈val=", stack.Pop())
	fmt.Println("出栈val=", stack.Pop())
	fmt.Println("出栈val=", stack.Pop())
}

func josephu() {
	head := &Josephu{
		num: 1,
	}
	//rand.Seed(time.Now().UnixNano())
	//for i := 0; i < 10; i++ {
	//	push(head, &Josephu{
	//		num: rand.Intn(1000) + 1,
	//	})
	//}
	for i := 2; i <= 5; i++ {
		push(head, &Josephu{
			num: i,
		})
	}
	//1 2 3 4 5
	//第一次
	list(head)
	delete(head, 2, 3)
}

func funcName4() {
	queue := &CircleQueue{
		HeadIndex: 0,
		TailIndex: 0,
		MaxSize:   5,
	}
	go func() {
		rand.Seed(time.Now().UnixNano())
		for {
			num := rand.Intn(100) + 1
			err := queue.Push(num)
			if err != nil {
				//fmt.Println("push 数据出错")
				//continue
			}
			time.Sleep(time.Microsecond * 500)
		}
		fmt.Println("生产数据一方结束")
	}()

	for i := 1; i <= 2; i++ {
		go func(i int) {
			for {
				if queue.Size() >= 0 {
					num, _ := queue.Pop()
					if num > 0 {
						fmt.Printf("%v号协程 服务 ---> %v 号客户\n", i, num)
					}
				}
				time.Sleep(time.Microsecond * 300)
			}
		}(i)
	}
	<-make(chan int)
}

func funcName3() {
	head := &CircleSingleLink{}
	node1 := &CircleSingleLink{
		no:   1,
		name: "漕河泾",
	}
	node2 := &CircleSingleLink{
		no:   2,
		name: "徐家汇",
	}
	node3 := &CircleSingleLink{
		no:   3,
		name: "张江高科",
	}
	node4 := &CircleSingleLink{
		no:   4,
		name: "五角场",
	}
	InsertCircleNode(head, node1)
	InsertCircleNode(head, node2)
	InsertCircleNode(head, node3)
	InsertCircleNode(head, node4)
	fmt.Println("删除前")
	ListCircleNode(head)
	head = DeleteCircleNode(head, 20)
	fmt.Println("删除后")
	ListCircleNode(head)
}

func funcName() {
	haed := &HeroNode{}
	l1 := &HeroNode{
		No:       1,
		Name:     "松江",
		NickName: "九亭",
	}
	l2 := &HeroNode{
		No:       2,
		Name:     "黄埔",
		NickName: "新天地",
	}
	l3 := &HeroNode{
		No:       3,
		Name:     "黄聪",
		NickName: "黄进",
	}
	//InsertNodeByNo(haed, l2)
	//InsertNodeByNo(haed, l1)
	//InsertNodeByNo(haed, l3)
	//DeleteNode(haed, 2)
	//PrintNode(haed)

	InsertNodeByNo2(haed, l2)
	InsertNodeByNo2(haed, l1)
	InsertNodeByNo2(haed, l3)
	fmt.Println("\n删除前")
	PrintNode(haed)
	DeleteNode2(haed, 2)
	fmt.Println("\n删除后")
	PrintNode(haed)
	fmt.Println("\n逆向打印")
	PrintNode2(haed)
}

func funcName2() {
	queue := &CircleQueue{
		HeadIndex: 0,
		TailIndex: 0,
		MaxSize:   5,
	}
	for {
		line := ""
		flat := 1
		if flat == 1 {
			fmt.Printf("请输入函数名以及相应的函数:")
			reader := bufio.NewReader(os.Stdin)
			buffer := make([]byte, 1024)
			i, _ := reader.Read(buffer)
			line = string(buffer[:i])
		} else {
			line = "ListQueue"
		}
		line = strings.Trim(line, "\r\n ")
		strs := strings.Split(line, " ")
		refVal := reflect.ValueOf(queue)
		funcName := strs[0]
		for i := 0; i < refVal.NumMethod(); i++ {
			name := reflect.TypeOf(queue).Method(i).Name
			if strings.ToLower(name) == strings.ToLower(strs[0]) {
				funcName = name
				break
			}
		}
		handed := refVal.MethodByName(funcName)
		if handed.IsValid() {
			var res []reflect.Value
			if len(strs) > 1 {
				p, _ := strconv.Atoi(strs[1])
				param := []reflect.Value{
					reflect.ValueOf(p),
				}
				res = handed.Call(param)
			} else {
				res = handed.Call(nil)
			}
			if len(res) > 0 {
				e, ok := res[len(res)-1].Interface().(error)
				if ok {
					fmt.Println(e.Error())
				}
			}
			if len(res) == 2 {
				i, ok := res[0].Interface().(int)
				if ok {
					fmt.Println("返回值为:", i)
				}
			}
		}
	}
}
